 // pages/biz/kill/orderList/orderList.js
//2019.6.11更改，全部统一拿status去判断
// 订单列表、订单详情接口：status字段
// 1.显示活动未开始标签
// 2.显示待支付标签、显示支付按钮
// 3.显示活动进行中标签、显示立即秒杀按钮
// 4.显示已支付标签
// 5.显示支付超时标签
// 6.显示待退款标签
// 7.显示已退款标签
import Serv from './orderListServ';
var regeneratorRuntime = require("../../../../lib/js/runtime-module.js")
import Util from '../../../../utils/util.js'
Page({
  
  /**
   * 页面的初始数据
   */
  data: {
    tabIndex:0,
    tabBar: { home: '../killHome/killHome', mine: '../orderList/orderList' },
    showTabBur:true,
    map:{
      1:{
        statusName:'活动未开始',
        statusTagColor:'tag_blue',
        buttonName:'联系客服',
        buttonStype:''
      },
      2:{
        statusName:'待支付',
        statusTagColor:'tag_yellow',
        buttonName:'立即支付',
        buttonStype:'bg_blue',
      },
      3: {
        statusName: '活动进行中',//状态名称
        statusTagColor: 'tag_yellow',// tag_grey 灰, tag_yellow,黄,tag_blue 蓝,tag_pink 粉红
        buttonName: '立即秒杀',//底部按钮，默认为‘联系客服’
        buttonStype: 'bg_blue'//空白底篮筐，bg_blue为蓝底白字
      },
      4:{
        statusName:'已支付',
        statusTagColor:'tag_blue',
        buttonName:'联系销售',
        buttonStype:'',
      },
      5:{
        statusName:'支付超时',
        statusTagColor:'tag_grey',
        buttonName:'联系客服',
        buttonStype:''
      },
      6:{
        statusName:'待退款',
        statusTagColor:'tag_pink',
        buttonName:'联系客服',
        buttonStype:'',
      },
      7:{
        statusName:'已退款',
        statusTagColor:'tag_grey',
        buttonName:'联系客服',
        buttonStype:''
      },
      8:{
        statusName:'活动已结束',
        statusTagColor:'tag_grey',
        //buttonName:'联系客服',
        buttonStype:''
      }
    },
    //1 进行中(已支付),2进行中(未支付),3未开始(已支付),4未开始(未支付),活动结束
    mapSec: { 
      1: {
        statusName: '活动进行中', //状态名称
        statusTagColor: 'tag_yellow',// tag_grey 灰, tag_yellow,黄,tag_blue 蓝,tag_pink 粉红
      },
      2: {
        statusName: '活动进行中',
        statusTagColor: 'tag_yellow',
      },
      3: {
        statusName: '活动未开始',
        statusTagColor: 'tag_blue',
      },
      4: {
        statusName: '活动未开始',
        statusTagColor: 'tag_blue',
      },
      5: {
        statusName: '活动结束',
        statusTagColor: 'tag_grey',
      }
    },
    mapflag: { 
      1: {
        statusName: '活动未开始', //状态名称
        statusTagColor: 'tag_blue',// tag_grey 灰, tag_yellow,黄,tag_blue 蓝,tag_pink 粉红
      },
      2: {
        statusName: '活动进行中',
        statusTagColor: 'tag_yellow',
      },
    },
    list:[],
    endTimeList: [],
    productDeType: [4, 5, 6, 7],
    productHouseType: [1, 2, 3]
  },
  
  changeTab:function(e){
    let index = e.currentTarget.dataset.tabindex
    if (this.data.tabIndex === index) {
      return false;
    } else {
      this.setData({
        tabIndex: index
      })
      if (index == 0) {
        this.getSeckillList({ orderFlag: 1 },0)
        this.setData({ orderFlag: 1, list: [] })
        clearTimeout(this.data.timeOut)
      } else if (index == 1) {
        this.setData({ orderFlag: 2, list: [] })
        this.getSeckillList({ orderFlag: 2 },0)
      } else if (index == 2) {
        this.getSeckillList({ orderFlag: 3 },0)
        this.setData({ orderFlag: 3, list: [] })
        clearTimeout(this.data.timeOut)
      } else if (index == 3) {
        this.getFavoriteSeckillList({},0)
        this.setData({ orderFlag: 4, list: [] })
        clearTimeout(this.data.timeOut)
      }
    }
  },
  
  //获取秒杀列表
  getSeckillList: async function (params,isPush){
    let { orderFlag } = params
    let { rows, success, msg, total, pageNo } = await Serv.querySeckillList(params)
    if(success){
      this.setData({ endTimeList: [], countDownList: [] })
      //处理返回数据
      rows.map(item=>{
        // if (item.startTime && item.endTime){
        //   let startTime = item.startTime.substr(0, item.startTime.length - 3).replace(/-/g, '.'),
        //     endTime = item.endTime.substr(0, item.endTime.length - 3).replace(/-/g, '.')
         
        //   item.startTime = startTime
        //   item.endTime = endTime
        // }
        let detail = JSON.parse(item.detail)
        item.detail = detail
        //获取支付超时时间
        // let timeList = this.data.endTimeList
        // timeList.push(item.payTimeOut)
        // this.setData({ endTimeList: timeList })

      })
      console.log(rows)
      if(isPush==0){
        this.setData({ list: rows, total, pageNo })
      }else{
        let list = this.data.list
        list.push(...rows)
        this.setData({ list: list, total, pageNo })
      }
      // if (this.data.orderFlag == 2) {
      //   this.countDown()
      // }
    }else{
      this.setData({ list: [] })
      msg = msg == "null" ? "暂无数据" : msg
      wx.showToast({
        title: msg,
        icon: 'none'
      })
    }
  },
  //获取我的关注秒杀列表
  getFavoriteSeckillList: async function (params,isPush){
    let { rows, success, msg, total, pageNo } = await Serv.queryFavoriteSeckill(params)
    if (success) {
      //处理返回数据
      rows.map(item => {
        if (item.startTime && item.startTime) {
          let startTime = item.startTime.substr(0, item.startTime.length - 3),
            endTime = item.endTime.substr(0, item.endTime.length - 3)
          startTime = startTime.replace(/-/g, '.')
          endTime = endTime.replace(/-/g, '.')
          item.startTime = startTime
          item.endTime = endTime
        }
        let activityStatus = item.activityStatus,
          status = item.status,
          type = item.type
        if (((type == 1 && status != 1) || (type == 2 && status)) && activityStatus == 2) {
          item.action = 1
        } else if (!type && !status && activityStatus == 2 || type == 1 && status == 1 && activityStatus == 2) {
          item.action = 2
        } else if (((type == 1 && status != 1) || (type == 2 && status)) && activityStatus == 1) {
          item.action = 3
        } else if (!type && !status && activityStatus == 1 || type == 1 && status == 1 && activityStatus == 1) {
          item.action = 4
        } else if (activityStatus == 3) {
          item.action = 5
        }
      })
      console.log(rows)
      if (isPush == 0) {
        this.setData({ list: rows, total, pageNo })
      } else {
        let list = this.data.list
        list.push(...rows)
        this.setData({ list: list, total, pageNo })
      }
    }else{
      this.setData({ list: [] })
    }
  },

  toMine(e){
    console.log(e)
  },
  // 订单列表跳转到订单详情页
  goDetail: function (e) {
    let {id,item,pid} = e.currentTarget.dataset
    if (item.orderFlag){
      wx.navigateTo({
        url: `../orderDetail/orderDetail?id=${id}&follow=1&tabIndex=${this.data.tabIndex}`
      })
    }else{
      wx.navigateTo({
        url: `/subPackages2/pages/kill/estateDetail/estateDetail?id=${pid}`,
      })
    }
    
  },
  // 关注列表跳转到秒杀活动详情
  goSeckillDetail(e){
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: `/subPackages2/pages/kill/estateDetail/estateDetail?id=${id}`
    })
  },
  //订单按钮
  toPay(e){
    let orderid = e.currentTarget.dataset.orderid
    let action = e.currentTarget.dataset.action
    let type = e.currentTarget.dataset.type
    let id = e.currentTarget.dataset.id
    let mobile = e.currentTarget.dataset.mobile
    let phone = e.currentTarget.dataset.phone
    // 20190611更改---去除action值，更改为status
    //1.显示活动未开始标签
    // 2.显示待支付标签、显示支付按钮
    // 3.显示活动进行中标签、显示立即秒杀按钮
    // 4.显示已支付标签
    // 5.显示支付超时标签
    // 6.显示待退款标签
    // 7.显示已退款标签
    if (action == 1 || action == 5 || action == 6 || action == 7) {
      wx.makePhoneCall({ phoneNumber: phone })
    } else if(action == 3){
      wx.navigateTo({
        url: `/subPackages2/pages/kill/rushPurchase/rushPurchase?actionType=1&seckillProductId=${id}`
      })
    } else if(action == 2){
      if(type==1){
        wx.navigateTo({
          url: `/subPackages2/pages/pay/payment/payment?seckillProductId=${id}&actionType=1`
        })
      }else{
        wx.navigateTo({
          url: `/subPackages2/pages/pay/payment/payment?seckillOrderId=${orderid}&seckillProductId=${id}&actionType=2`
        })
      }
      
    } else if (action == 4) {
      wx.makePhoneCall({ phoneNumber: mobile })
    }
  },
  //关注按钮
  toNext(e){
    let action = e.currentTarget.dataset.action,
      id = e.currentTarget.dataset.id
    if (action == 1) {
      wx.navigateTo({
        url: `/subPackages2/pages/kill/rushPurchase/rushPurchase?actionType=1&seckillProductId=${id}`
      })
    } else if (action == 4) {
      wx.navigateTo({
        url: `/subPackages2/pages/pay/payment/payment?actionType=1&seckillProductId=${id}`
      })
    }
  },
  timeFormat(param) {//小于10的格式化函数
    return param < 10 ? '0' + param : param;
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let orderFlag = options.tabIndex ? Number(options.tabIndex) + 1 : 1
    this.setData({ orderFlag, seckillActivityId: options.seckillActivityId||'', tabIndex: options.tabIndex||0 })
    if (options.type && options.type==1){
      this.setData({showTabBur:false})
    }
  },
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },
  
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let orderFlag = this.data.orderFlag
    if (orderFlag == 1 || orderFlag == 3) {
      this.getSeckillList({ orderFlag: this.data.orderFlag },0)
    } else if (orderFlag == 2) {
      this.getSeckillList({ orderFlag: this.data.orderFlag },0)
    } else if (orderFlag == 4) {
      this.getFavoriteSeckillList({ pageNo: 1, pageSize: 10 }, 0)
    }
  },
  
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    clearTimeout(this.data.timeOut)
  },
  
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    clearTimeout(this.data.timeOut)
  },
  
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },
  
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let total = Number.parseInt(this.data.total) // 总条数
    let totalPage = 1 // 总页数
    if (total % 10 === 0) {
      totalPage = Number.parseInt(total / 10)
    } else {
      totalPage = Number.parseInt(total / 10) + 1
    }
    let pageNo = Number.parseInt(this.data.pageNo) // 当前页数
        // debugger
    if (pageNo < totalPage && (pageNo + 1) <= totalPage) { // 如果当前条数小于总条数
      if (this.data.orderFlag != 4) {
        let pageParams = {
          pageNo: pageNo + 1,
          pageSize: 10,
          orderFlag: this.data.orderFlag
        }
        this.getSeckillList(pageParams, 1)
      } else {
        let pageParams = {
          pageNo: pageNo + 1,
          pageSize: 10
        }
        this.getFavoriteSeckillList(pageParams, 1)
      }
    }
  }
})
